//
//  LSZComposeToolBar.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/18.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit
//发微博toolbar按钮的枚举类型
enum LSZComposeToolBarButtonType: Int {
//    图片
    case Picture = 0
//    @
    case Mention = 1
//    话题
    case Trend = 2
//    表情
    case Emoticon = 3
//    加号
    case Add = 4
}
class LSZComposeToolBar: UIStackView {
//    定义点击toolbar按钮的闭包
    var selectedToolBarButtonClose: ((type: LSZComposeToolBarButtonType) -> ())?
//    表情按钮
    var emotionButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
//        UIStackView控件本身不具备渲染效果,依赖子控件的显示
//        backgroundColor = UIColor.yellowColor()
        
//        设置布局横向的
        axis = .Horizontal
//        子控件的填充方式(相等的填充)
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emotionButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    }
    
//    添加子控件的公用方法
    private func addChildButton(imageName: String, type: LSZComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
//        获取枚举的原始值
        button.tag = type.rawValue
//        添加点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
//        设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
//        设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
//        取消高亮的状态
        button.adjustsImageWhenHighlighted = false
//        UIStackView下这么添加
        addArrangedSubview(button)
        return button
    }
    
//     MARK: 点击事件
    @objc private func buttonAction(button: UIButton) {
//        通过枚举原始值获取枚举成员
        let type = LSZComposeToolBarButtonType(rawValue: button.tag)!
//        执行闭包回调
        selectedToolBarButtonClose?(type: type)
    }
    
//    根据外界键盘类型设置不同的图标
    func showEmotionIcon(isEmotion : Bool){
//        如果是表情键盘类型,那么显示键盘icon
//        如果是键盘类型,那么显示表情icon
        if isEmotion {
            emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        } else {
            emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
        
    }
    
    

}
